package com.ruoyi.business.controller;

import com.ruoyi.business.service.ITRegisterNeedService;
import com.ruoyi.business.vo.TRegisterNeedVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 招生信息Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "招生信息Controller")
@RestController
@RequestMapping("/business/registerNeed")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TRegisterNeedController extends BaseController {

    private final ITRegisterNeedService tRegisterNeedService;

    @ApiOperation("查询招生信息列表")
    //@PreAuthorize("@ss.hasPermi('business:registerNeed:list')")
    @GetMapping("/list")
    public TableDataInfo<TRegisterNeedVo> list(TRegisterNeedVo entity) {
        entity.setUserId(SecurityUtils.getLoginUser().getUserId());
        return tRegisterNeedService.queryList(entity);
    }

    @ApiOperation("查询招生信息列表")
    //@PreAuthorize("@ss.hasPermi('business:registerNeed:list')")
    @GetMapping("/index/list")
    public TableDataInfo<TRegisterNeedVo> indexList(TRegisterNeedVo entity) {
        return tRegisterNeedService.queryList(entity);
    }

    @ApiOperation("查询招生信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TRegisterNeedVo entity) {
        return AjaxResult.success("查询成功", tRegisterNeedService.queryAll(entity));
    }

    @ApiOperation("导出招生信息列表")
    @PreAuthorize("@ss.hasPermi('business:registerNeed:export')")
    @Log(title = "招生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRegisterNeedVo entity) {
        entity.setUserId(SecurityUtils.getLoginUser().getUserId());
        List<TRegisterNeedVo> list = tRegisterNeedService.queryAll(entity);
        ExcelUtil<TRegisterNeedVo> util = new ExcelUtil<>(TRegisterNeedVo.class);
        util.exportExcel(response, list, "招生信息数据");
    }

    @ApiOperation("获取招生信息详细信息")
    //@PreAuthorize("@ss.hasPermi('business:registerNeed:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tRegisterNeedService.queryById(id));
    }

    @ApiOperation("新增招生信息")
    @PreAuthorize("@ss.hasPermi('business:registerNeed:add')")
    @Log(title = "招生信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TRegisterNeedVo entity) {

        return toAjax(tRegisterNeedService.save(entity));
    }

    @ApiOperation("修改招生信息")
    @PreAuthorize("@ss.hasPermi('business:registerNeed:edit')")
    @Log(title = "招生信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TRegisterNeedVo entity) {
        return toAjax(tRegisterNeedService.updateById(entity));
    }

    @ApiOperation("删除招生信息")
    @PreAuthorize("@ss.hasPermi('business:registerNeed:remove')")
    @Log(title = "招生信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tRegisterNeedService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
